﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using YiSha.Data.Repository;
using YiSha.Entity.SystemManage;
using YiSha.Enums;
using YiSha.Model.Param.SystemManage;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Service.SystemManage
{
    public class AutoJobService : RepositoryFactory
    {

        #region 获取数据
        public async Task<TData<List<AutoJobEntity>>> GetList(AutoJobListParam param)
        {
            var obj = new TData<List<AutoJobEntity>>();
            var expression = ListFilter(param);
            var list = SqlSugarAdapters.GetList(expression);
            obj.Data = list;
            obj.MsgCode = MsgCode.Success;
            return obj;
        }

        public async Task<TData<List<AutoJobEntity>>> GetPageList(AutoJobListParam param, Pagination pagination)
        {
            var obj = new TData<List<AutoJobEntity>>();
            var expression = ListFilter(param);
            var list = SqlSugarAdapters.GetPager(expression, pagination);
            obj.Data = list.Item2;
            obj.Total = list.Item1;
            return obj;
        }
        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AutoJobEntity> GetEntity(string id)
        {
            var info = SqlSugarAdapters.GetEntity<AutoJobEntity>(s => s.Id == id);
            return info;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool ExistJob(AutoJobEntity entity)
        {
            var expression = ExtLinq.True<AutoJobEntity>();
            expression = expression.And(t => t.Valid);
            if (entity.Id.IsEmpty())
            {
                expression = expression.And(t => t.JobGroupName == entity.JobGroupName && t.JobName == entity.JobName);
            }
            else
            {
                expression = expression.And(t => t.JobGroupName == entity.JobGroupName && t.JobName == entity.JobName && t.Id != entity.Id);
            }
            return this.BaseRepository().IQueryable(expression).Count() > 0 ? true : false;
        }
        #endregion

        #region 提交数据
        public async Task<TData> SaveForm(AutoJobEntity entity)
        {
            var obj = new TData();
            try
            {
                if (entity.Id.IsEmpty())
                {
                    await entity.Create();
                    SqlSugarAdapters.Insert<AutoJobEntity>(entity);
                }
                else
                {
                    await entity.Modify();
                    SqlSugarAdapters.Update<AutoJobEntity>(entity);
                }
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }

        public async Task<TData> DeleteForm(List<string> ids)
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.Delete<AutoJobEntity>(s => ids.Contains(s.Id));
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        #endregion

        #region 私有方法
        private Expression<Func<AutoJobEntity, bool>> ListFilter(AutoJobListParam param)
        {
            var expression = ExtLinq.True<AutoJobEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.JobName))
                {
                    expression = expression.And(t => t.JobName.Contains(param.JobName));
                }
            }
            return expression;
        }
        #endregion
    }
}

